package com.spzx.product.controller;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;
    @Operation(summary = "分页查询spu列表")
    @GetMapping("list")
    public TableDataInfo list(){
        startPage();
        List<Product> list = productService.getlist();
        return getDataTable(list);
    }
    @PostMapping
    @Operation(summary = "保存商品（SPU、SKU列表、SPU详情、库存）")
    public AjaxResult saveProduct(@RequestBody Product product) {
        productService.saveProduct(product);
        return success();
    }
    @Operation(summary = "商品上架或下架")
    @GetMapping("/updateStatus/{spuId}/{status}")
    public AjaxResult updateStatus(
            @PathVariable("spuId") Long spuId,
            @PathVariable("status") Integer status
    ) {
        if(status != 1 && status != -1){
            throw new ServiceException("状态值有误");
        }
        productService.updateStatus(spuId, status);
        return success();
    }
    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        productService.removeProduct(ids);
        return success();
    }
}
